package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.aicall.*;
import com.miplus.ccrm.core.controller.vo.ficase.ImpDelReqVo;
import com.miplus.ccrm.core.controller.vo.ficase.ImpResultVo;
import com.miplus.ccrm.core.controller.vo.strategy.PreViewTrigReqVo;
import com.miplus.ccrm.core.pojo.ObCallCase;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.aicall.ObCallCaseService;
import com.miplus.ccrm.core.service.aicall.ObCallHistService;
import com.miplus.ccrm.core.service.strategy.ObJobService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-10.智能外呼服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/aicall")
public class AiCallController {

    @Resource
    private ObCallCaseService callCaseService;
    @Resource
    private ObCallHistService callHistService;
    @Resource
    private JCommonData jCommonData;

    @Resource
    private ObJobService obJobService;

    /**
     * 取外呼数据
     */
    @PostMapping("/dialList")
    public R<PageData<ObCallListResVo>> dialList(@RequestBody @Validated ObCallListReqVo callListReqVo){
        return R.SuccessPage(callCaseService.dialList(callListReqVo));
    }

    /**
     * 取呼叫历史
     */
    @GetMapping("/dialhist")
    public R<List<ObCallHistResVo>> dialhist(@RequestParam @NotNull String caseid){
        return R.Success(callHistService.dialHist(Long.parseLong(caseid)));
    }

    /**
     * 修改数据状态
     * 1=就绪，0=未就绪
     */
    @PostMapping("/upDataStatus")
    public R<CmdResVo> upDataStatus(@RequestBody @Validated UpDataStatusReqVo statusReqVo){
        return R.Success(callCaseService.updateDataStatus(statusReqVo));
    }

    /**
     * 修改CALL状态
     * 1=就绪，0=未就绪
     */
    @PostMapping("/upCallStatus")
    public R<CmdResVo> upCallStatus(@RequestBody @Validated UpCallStatusVo callStatusVo){
        return R.Success(callCaseService.updateCallStatus(callStatusVo));
    }

    /**
     * 取触发器对应的外呼数据导入预览
     */
    @PostMapping("/quyImpObHist")
    public R<PageData<ImpObCaseResVo>> quyImpObHist(@RequestBody @Validated PreViewTrigReqVo trigReqVo){
        return R.SuccessPage(callCaseService.quyImpObHist(trigReqVo));
    }

    /**
     * 导入外呼数据
     */
    @PostMapping("/importObCase")
    public R<ImpResultVo> importObCase(@RequestBody @Validated ImpObCallReqVo obCallReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(callCaseService.impObCase(usrid,obCallReqVo));
    }

    /**
     * 外呼任务统计表
     */
    @PostMapping("/repJobTotal")
    public R<PageData<TotalRepResVo>> repJobTotal(@RequestBody @Validated TotalRepReqVo repReqVo){
        return R.SuccessPage(callCaseService.repJobTotal(repReqVo));
    }

    /**
     * 外呼批次统计表
     */
    @PostMapping("/repJobBatch")
    public R<PageData<BatchRepResVo>> repJobBatch(@RequestBody @Validated BatchRepReqVo repReqVo){
        return R.SuccessPage(callCaseService.repJobBatch(repReqVo));
    }

    /**
     * 导出外呼任务统计表
     */
    @PostMapping("/expJobTotal")
    public R<CmdResVo> expJobTotal(@RequestBody @Validated TotalRepReqVo repReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(callCaseService.expJobTotal(user,repReqVo,response));
    }

    /**
     * 导出外呼批次统计表
     */
    @PostMapping("/expJobBatch")
    public R<CmdResVo> expJobBatch(@RequestBody @Validated BatchRepReqVo repReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(callCaseService.expJobBatch(user,repReqVo,response));
    }

    /**
     * 外呼服务启动/停止
     */
    @PostMapping("/dialAdmin")
    public R<CmdResVo> dialAdmin(@RequestBody @Validated DialAdminCmdVo cmdVo){
        return R.Success(callCaseService.dialAdmin(cmdVo));
    }

    /**
     * 取JOB状态（启动/停止）
     */
    @GetMapping("/getJobStatus")
    public R<CmdResVo> getJobStatus(@RequestParam @NotNull String jobid){
        return R.Success(callCaseService.getDialJobStatus(jobid));
    }

    /**
     * 获取外呼Job一览表
     */
    @PostMapping("/jobList")
    public R<PageData<ObJobListResVo>> jobList(@RequestBody @Validated ObJobListReqVo obJobListReqVo){
        return R.SuccessPage(obJobService.getJobList(obJobListReqVo));
    }

    /**
     * 获取坐席Job一览表
     */
    @GetMapping("/agentJobList")
    public R<List<LabelResVo>> agentJobList(@RequestParam @NotNull String agentId){
        return R.Success(obJobService.selectAgentJobList(agentId));
    }

    /**
     * 取外呼数据记录
     */
    @GetMapping("/getObCaseDetail")
    public R<ObCallCase> getObCaseDetail(@RequestParam @NotNull Long caseId){
        return R.Success(callCaseService.getObCallCaseDetail(caseId));
    }

    /**
     * AiCall 94AI外呼推送列表
     * @param reqVo
     * @return
     */
    @PostMapping("/aiCallSendBatchPage")
    public R<PageData<BatchAiCallResVo>>aiCallSendBatchPage(@RequestBody @NotNull BatchAiCallReqVo reqVo){

        return R.SuccessPage(callCaseService.aiCallSendBatchPage(reqVo));
    }
    /**
     * AiCall 94AI外呼推送列表 明细
     * @param reqVo
     * @return
     */
    @PostMapping("/aiCallSendDetailPage")
    public R<PageData<BatchDetailAiCallResVo>>aiCallSendDetailPage(@RequestBody @NotNull BatchAiCallReqVo reqVo){
        JSysUser user= jCommonData.getCurrUsr();

        return R.SuccessPage(callCaseService.aiCallSendDetailPage(user,reqVo));
    }

}
